require(['./config'], () => {
    require(['template','url','cookie','header'], (template,url,cookie) => {
        class Log {
            constructor () {
                this.addfooter()
                this.userLog()
                this.showName()
                this.tuichu()
                this.drag()
            }
            addfooter(){
                $('footer').load('../html/footer.html')
            }
            userLog(){
                let _this = this
                $('.btn').on('click',function(){
                    let phone = $('.phone').val(),
                    passone = $('.passone').val()
                    
                    let user =localStorage.getItem('user')
                    user = JSON.parse(user)
                    
                    $.each(user,function(i,item){
                        //判断输入信息是否存在
                        if(phone === item.phone && passone === item.passone){
                            //判断是否拖动滑块
                            if($('.tuan').html() == "ok"){
                                //判断是否勾选协议
                                if($('.sour').prop('checked')){
                                    alert('登陆成功！')
                                    $.cookie('phone',`${phone}`,{expires:1,path:'/'})
                                    $.cookie('passone',`${passone}`,{expires:1,path:'/'})
                                    _this.showName()
                                    window.location.replace('../index.html')
                                }else{
                                    alert('请仔细阅读协议！');
                                }
                            }else{
                                alert('请拖动滑块验证！');
                            }
                        }
                    })
                })
            }
            drag(){
                let drag = $('.drag'),
                    tuo = $('.tuo'),
                    tuan = $('.tuan')
                
                tuan.on('mousedown',function(){
                    //获取鼠标光标位置坐标
                   
                    tuan.on('mousemove',(e)=>{
                        let x = e.offsetX,
                            left = tuan.position().left+x

                            if(left >= drag.width()-tuan.width()){
                                tuan.html('ok')
                                tuan.css('color','green')
                                left = drag.width()-tuan.width()
                            }
                        tuo.css('width',left)
                        tuan.css('left',left)
                       
                        
                    })
                    tuan.on('mouseup',()=>{
                        tuan.on('mousemove',null)
                    })
                       
                    
                })
            }
            showName(){
                if( $.cookie('phone')){
                  $('.lasttwo').hide()
                  $('.lastlog').show()
                }else{
                  $('.lasttwo').show()
                  $('.lastlog').hide()
                }
           }
           tuichu(){
            let _this = this
            $(".lastlog span").on('click',function(){
             $.cookie('phone','',{expires:-1,path:'/'})
             $.cookie('passone','',{expires:-1,path:'/'})
             _this.showName()
            })
          }
        }
        new Log()
    })
})